﻿Imports FinanceiroBLL
Imports FinanceiroDAL
Imports EstoqueBLL
Public Class ContasaPagarFRM
    Public Op As Char
    Public finaliza As String
    Public var, var2 As Integer
    Public sacado As String
    Public valortotal As Decimal
    Dim tabItens As New DataTable
    Public codPes, codccc, codigo, codcompras, st, parcela As Integer
    Public objCompras As New ComprasBLL
    Dim refnota, pes As Integer
    Dim objBLL As New ContasPagarBLL
    Dim objItBLL As New ItensCtasPagarBLL
    Dim objCCusto As New CentroCustoBLL
#Region "Botão Parcelamento"
    Private Sub BParcelamento_Click(sender As System.Object, e As System.EventArgs) Handles BParcelamento.Click
        Dim valorparcela As Decimal
        finaliza = "1"
        Decimal.TryParse(TXTVrParcela.Text.Replace("R$", ""), valorparcela)
        Try
            If (RBPrazo.Checked = False And RBVista.Checked = True) Or (RBPrazo.Checked = True And RBVista.Checked = False) Then
                If (TXTVrParcela.Text <> "" And valorparcela <> 0) Then
                    If (TXTFornPrestador.Text <> "") Then
                        If (CBContas.SelectedValue <> Nothing And CBFormaPgto.SelectedValue <> Nothing) Then
                            TSBSalvar_Click(Nothing, Nothing)
                            sacado = TXTFornPrestador.Text
                            valortotal = (TXTVrParcela.Text.Replace("R$", ""))
                            var2 = Val(TXTNumref.Text)
                            Dim diavenc As Date = DTPDtVencimento.Text
                            Dim frm As New ParcelamentosFRM
                            frm.codigo = var
                            frm.diavencim = diavenc
                            frm.doc = TXTBolChequeTit.Text
                            frm.numparcelas = parcela
                            frm.objfrmcontaspagar = Me
                            frm.ShowDialog()
                            finaliza = "OK"
                            If (Not IsNothing(objCompras)) Then
                                If (objCompras.com_codigo > 0) Then
                                    MessageBox.Show("Lançamento do compras.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                    limpar()
                                    'carregar o compras
                                    Dim objfornecedor As New FornecedorBLL(objCompras.for_codigo)
                                    TXTFornPrestador.Text = objfornecedor.razao
                                    TXTCentroCusto.Text = "Fornecedor"
                                    TXTValTitulo.Text = FormatCurrency(objCompras.com_valortotal)
                                    TXTNumref.Text = objCompras.com_numnota
                                    DTPDtEmissao.Value = objCompras.com_dtemissao
                                    codccc = 1
                                    objCompras = Nothing
                                    TXTValTitulo_Leave(Nothing, Nothing)
                                    finaliza = "OK"
                                Else
                                    Close()
                                End If
                            End If

                        Else
                            MsgBox("Conta ou forma de pagamento não selecionado.", MsgBoxStyle.Exclamation)
                        End If
                    Else
                        MsgBox("Campo Fornecedor / Prestador não preenchido!", MsgBoxStyle.Exclamation)
                        TXTFornPrestador.SelectAll()
                    End If
                Else
                    MsgBox("Valor inválido!", MsgBoxStyle.Exclamation)
                    TXTValTitulo.SelectAll()
                End If
            Else
                MsgBox("Escolha uma condição de pagamento.", MsgBoxStyle.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "Load"
    Private Sub ContasPagarFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        limpar()
        If Op = "N" Then 'novo
            CarregaCombo()
        End If
        If (Not IsNothing(objCompras)) Then
            If (objCompras.com_codigo > 0) Then
                CarregaCombo()
                If (objCompras.com_valorfrete > 0) Then
                    TXTValTitulo.Text = FormatCurrency(objCompras.com_valorfrete)
                    TXTValTitulo_Leave(Nothing, Nothing)
                    MessageBox.Show("Lançamento do frete.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("Lançamento do compras.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    limpar()
                    'carregar o compras
                    Dim objfornecedor As New FornecedorBLL(objCompras.for_codigo)
                    TXTFornPrestador.Text = objfornecedor.razao
                    TXTCentroCusto.Text = "Fornecedor"
                    TXTValTitulo.Text = FormatCurrency(objCompras.com_valortotal)
                    TXTNumref.Text = objCompras.com_numnota
                    DTPDtEmissao.Value = objCompras.com_dtemissao
                    codccc = 1
                    objCompras = Nothing
                    TXTValTitulo_Leave(Nothing, Nothing)

                End If
            End If
        End If
        If (Op = "M") Then 'movimentação
            CarregaCombo()

        End If
    End Sub
#End Region
#Region "Carrega combo"
    Sub CarregaCombo()
        objconexao.carregaCombo(CBFormaPgto, "Generico.Gen_FormaPagamento", "pag_codigo", "pag_descricao", "sta_codigo =1", "")
        objconexao.carregaCombo(CBContas, "Financeiro.Ofi_Contas", "con_codigo", "con_descricao", "sta_codigo=1", "")
        CBFormaPgto.SelectedValue = -1
        CBContas.SelectedValue = -1
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        LBCodigo.Text = ""
        TXTCentroCusto.Text = ""
        TXTNumref.Text = ""
        CBFormaPgto.SelectedValue = -1
        DTPDtEmissao.Text = ""
        TXTFornPrestador.Text = ""
        TXTObservacao.Text = ""
        TXTValTitulo.Text = "0,00"
        TXTDesconto.Text = "0,00"
        TXTJuros.Text = "0,00"
        TXTVrParcela.Text = "0,00"
        CBContas.SelectedValue = -1
        TXTBolChequeTit.Text = ""
    End Sub
#End Region
#Region "Fechar"
    Private Sub TSBFechar_Click(sender As System.Object, e As System.EventArgs) Handles TSBFechar.Click
        Me.Close()
        objBLL = Nothing
        limpar()
    End Sub
#End Region
#Region "Localiza"
    Private Sub BLocaliza_Click(sender As System.Object, e As System.EventArgs) Handles BLocaliza.Click
        Dim objF As New PessoaCON
        objF.type = "ret"
        objF.ShowDialog()
        TXTFornPrestador.Text = objF.fornprest
        codPes = objF.codpes
        TXTCentroCusto.Text = objF.centrocusto
        codccc = objF.codccc
    End Sub
#End Region
#Region "Valida"
    Sub valida()
        Try
            If (TXTNumref.Text <> "") Then
                refnota = TXTNumref.Text
            Else
                refnota = Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "Salvar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Dim valorfinal As Decimal = 0
        Decimal.TryParse(TXTVrParcela.Text.Replace("R$", ""), valorfinal)
        Try
            valida()
            If (RBPrazo.Checked = False And RBVista.Checked = True) Or (RBPrazo.Checked = True And RBVista.Checked = False) Then
                If (TXTFornPrestador.Text <> "") Then
                    If (valorfinal > 0) Then
                        If (TXTValTitulo.Text <> "" And TXTVrParcela.Text <> "") Then
                            If (CBContas.SelectedValue <> Nothing And CBFormaPgto.SelectedValue <> Nothing) Then
                                objBLL = New ContasPagarBLL(codigo, codccc, refnota, Date.Parse(DTPDtEmissao.Text), TXTObservacao.Text, TXTFornPrestador.Text)
                                Dim objItPagar As New ItensCtasPagarBLL
                                If (st = 4) Then
                                    objItPagar.icp_datapagamento = Nothing
                                Else
                                    objItPagar.icp_datapagamento = DTPDtVencimento.Text
                                End If
                                objItPagar.icp_valor = TXTValTitulo.Text
                                objItPagar.pag_codigo = CBFormaPgto.SelectedValue
                                objItPagar.icp_datavencimento = DTPDtVencimento.Text
                                objItPagar.icp_valorpago = TXTVrParcela.Text 'campo total + jam -desc
                                objItPagar.icp_desconto = TXTDesconto.Text
                                objItPagar.icp_jam = TXTJuros.Text
                                objItPagar.icp_parcela = parcela 'num de parcela
                                objItPagar.con_codigo = CBContas.SelectedValue
                                objItPagar.icp_boletocheque = TXTBolChequeTit.Text
                                objItPagar.sta_codigo = st
                                objBLL.lCtasPagar.Add(objItPagar)
                                var = objBLL.Inserir()
                                var = objBLL.ctp_codigo
                                MsgBox("Dados gravados com sucesso!", MsgBoxStyle.Information)
                                If (Not IsNothing(objCompras)) Then
                                    If (objCompras.com_codigo > 0) Then
                                        If (finaliza = "") Then
                                            MessageBox.Show("Lançamento do compras.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                            limpar()
                                            'carregar o compras
                                            Dim objfornecedor As New FornecedorBLL(objCompras.for_codigo)
                                            TXTFornPrestador.Text = objfornecedor.razao
                                            TXTCentroCusto.Text = "Fornecedor"
                                            TXTValTitulo.Text = FormatCurrency(objCompras.com_valortotal)
                                            TXTNumref.Text = objCompras.com_numnota
                                            DTPDtEmissao.Value = objCompras.com_dtemissao
                                            codccc = 1
                                            objCompras = Nothing
                                            TXTValTitulo_Leave(Nothing, Nothing)
                                            finaliza = "OK"
                                        End If
                                    Else
                                        finaliza = "OK"
                                        Close()
                                    End If
                                Else
                                    finaliza = "OK"
                                    Close()
                                End If

                                ' limpar()
                            Else
                                MsgBox("Conta ou forma de pagamento não selecionados!", MsgBoxStyle.Exclamation)
                            End If
                        Else
                            MsgBox("Campo vazio", MsgBoxStyle.Exclamation)
                            TXTValTitulo.SelectAll()
                        End If
                    Else
                        MsgBox("Valor inválido!", MsgBoxStyle.Information)
                        TXTValTitulo.SelectAll()
                    End If
                Else
                    MsgBox("Selecione um fornecedor/prestador!", MsgBoxStyle.Exclamation)
                    TXTFornPrestador.SelectAll()
                End If
            Else
                MsgBox("Escolha uma condição de pagamento.", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
            DTPDtVencimento.Focus()
        Finally

        End Try
    End Sub
#End Region
#Region "Atualiza Valor"
    Sub AtualizaValor()
        Dim ValorTotal As Decimal = 0
        Dim valtitulo As Decimal = 0
        Dim desc, jam As Decimal
        Try
            If (Decimal.TryParse(TXTValTitulo.Text.Replace("R$", ""), valtitulo)) Then
            Else : valtitulo = "0,00"
            End If
            If (Decimal.TryParse(TXTDesconto.Text.Replace("R$", ""), desc)) Then
            Else : desc = ("0,00")
            End If
            If (Decimal.TryParse(TXTJuros.Text.Replace("R$", ""), jam)) Then
            Else : jam = "0,00"
            End If
            If (Decimal.TryParse(TXTVrParcela.Text.Replace("R$", ""), ValorTotal)) Then
            Else : ValorTotal = "0,00"
            End If

            If (desc > valtitulo) Then
                TXTVrParcela.Text = valtitulo - desc
                MsgBox("Valor de Desconto maior que o valor do título", MsgBoxStyle.Exclamation)
                TXTDesconto.Select()
                TXTDesconto.SelectAll()
            Else
                TXTVrParcela.Text = (valtitulo + jam) - desc
            End If
            If TXTValTitulo.Text <> "" Then
                TXTValTitulo.Text = FormatCurrency(TXTValTitulo.Text)
            Else
                TXTValTitulo.Text = "0,00"
            End If
            If TXTDesconto.Text <> "" Then
                TXTDesconto.Text = FormatCurrency(TXTDesconto.Text)
            Else
                TXTDesconto.Text = "0,00"
            End If
            If TXTJuros.Text <> "" Then
                TXTJuros.Text = FormatCurrency(TXTJuros.Text)
            Else
                TXTJuros.Text = "0,00"
            End If

            TXTVrParcela.Text = FormatCurrency(TXTVrParcela.Text)
        Catch ex As Exception
            MsgBox("Formato incorreto nos campos!")
        End Try
    End Sub
#End Region
#Region "TXTDesconto"
    Private Sub TXTDesconto_Leave(sender As Object, e As System.EventArgs) Handles TXTDesconto.Leave
        AtualizaValor()
    End Sub

    Private Sub TXTDesconto_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TXTDesconto.MouseUp
        TXTDesconto.SelectAll()
    End Sub
    Private Sub TXTDesconto_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTDesconto.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
        End If
    End Sub
#End Region
#Region "TXTValTitulo"
    Private Sub TXTValTitulo_Leave(sender As Object, e As System.EventArgs) Handles TXTValTitulo.Leave
        AtualizaValor()
    End Sub

    Private Sub TXTValTitulo_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TXTValTitulo.MouseUp
        TXTValTitulo.SelectAll()
    End Sub

    Private Sub TXTValTitulo_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValTitulo.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTDesconto.Select()
            TXTValTitulo.Text = FormatCurrency(TXTValTitulo.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub
#End Region
#Region "TXTJuros"
    Private Sub TXTJuros_Leave(sender As Object, e As System.EventArgs) Handles TXTJuros.Leave
        AtualizaValor()
    End Sub
    Private Sub TXTJuros_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TXTJuros.MouseUp
        TXTJuros.SelectAll()
    End Sub

    Private Sub TXTJuros_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTJuros.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTJuros.Text = FormatCurrency(TXTJuros.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub
#End Region
#Region "TXTVlrParcela"
    Private Sub TXTVrParcela_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTVrParcela.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TXTVrParcela.Text = FormatCurrency(TXTVrParcela.Text.Replace("R$", "").Replace(" ", ""))
        End If
    End Sub
#End Region
#Region "Propriedades"
    Private Sub CKPunica_GotFocus(sender As Object, e As System.EventArgs)
        valida()
    End Sub
    Private Sub CKPunica_Leave(sender As Object, e As System.EventArgs)
        valida()
    End Sub


    Private Sub TXTBolChequeTit_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTBolChequeTit.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And Not e.KeyChar = "-" And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub TXTNumref_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTNumref.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And Not e.KeyChar = "-" And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub ContasPagarFRM_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub DTPDtVencimento_Leave(sender As System.Object, e As System.EventArgs) Handles DTPDtVencimento.Leave
        validadata()
    End Sub
    Sub validadata()
        If DTPDtVencimento.Value < (Date.Now).ToShortDateString Then
            MsgBox("Data de vencimento menor que a data atual", MsgBoxStyle.Exclamation)
            DTPDtVencimento.Select()
        End If
    End Sub
#End Region
#Region "Forma de pgto validating"
    Private Sub CBFormaPgto_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles CBFormaPgto.Validating
        If (CBFormaPgto.SelectedValue > 0) Then
            If (CBFormaPgto.SelectedValue = 1 Or CBFormaPgto.SelectedValue = 3 Or CBFormaPgto.SelectedValue = 4 Or CBFormaPgto.SelectedValue = 8) And (RBPrazo.Checked) Then
                BParcelamento.Visible = True
            End If
            If CBFormaPgto.SelectedValue = 2 Or CBFormaPgto.SelectedValue = 5 Or CBFormaPgto.SelectedValue = 6 Or CBFormaPgto.SelectedValue = 7 Or CBFormaPgto.SelectedValue = 9 And RBVista.Checked Then 'a vista
                BParcelamento.Visible = False
                RBVista.Checked = True
                RBPrazo.Enabled = False
            End If
        End If
    End Sub
#End Region
#Region "RADIO BUTTON"
    Private Sub RBVista_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RBVista.CheckedChanged
        If RBVista.Checked Then
            BParcelamento.Visible = False
            st = 6 ' a vista
            parcela = 1
        Else
            RBPrazo.Checked = True
            st = 4
            parcela = 1
        End If
    End Sub
    Private Sub RBPrazo_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RBPrazo.CheckedChanged
        If RBPrazo.Checked Then
            BParcelamento.Visible = True
            st = 4
            parcela = 1
            TSBSalvar.Visible = False
        Else
            st = 6
            parcela = 1
            TSBSalvar.Visible = True
        End If
    End Sub
#End Region
End Class